package com.wing.device.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.device.model.entity.Maintenance;
import com.wing.device.model.entity.MaintenanceExecute;
import com.wing.device.model.response.MaintenanExecuteDetailVo;
import com.wing.device.model.response.MaintenanceVo;
import com.wing.device.service.DeviceMaintenanceClaimService;
import com.wing.device.service.DeviceMaintenanceExecuteDetailService;
import com.wing.device.service.DeviceMaintenanceExecuteService;
import com.wing.device.service.DeviceMaintenanceService;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.1.0
 * @Author hz
 * @Date 2024-08-19 15:13
 **/
@Validated
@Api(value = "Admin-设备维修保养认领记录管理-我的", tags = "#/device/maintenanceClaimMy")
@RestController("maintenanceClaimControllerMy")
@RequestMapping("device/admin/maintenanceClaimMy")
@Slf4j
public class DeviceMaintenanceClaimMyController {

    @Resource
    private DeviceMaintenanceClaimService deviceMaintenanceClaimService;

    @ApiOperation(value = "查询设备维修保养认领记录列表-我的", notes = "搜索")
    @GetMapping("/list")
    public PageResult<Maintenance> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String status,
            String taskNo,
            String departmentSearchClaimId,
            String postIdSearchClaim,
            String createName,
            String deviceNo,
            String claimName,
            String postId
            ){
        Page<Maintenance> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("status", status);
        map.put("taskNo", taskNo);
        map.put("postId", postId);
        map.put("departmentSearchClaimId", departmentSearchClaimId);
        map.put("postIdSearchClaim", postIdSearchClaim);
        //录入人
        map.put("createName", createName);
        map.put("deviceNo", deviceNo);
        //认领人
        map.put("claimName", JwtUtils.getLoginUsername());
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        map.put("databaseUser", databaseUser);
        //执行人 默认自己
        IPage<Maintenance> list = deviceMaintenanceClaimService.pageList4app(pg, map);
        List<Maintenance> records = list.getRecords();
        records.stream().forEach(info->{
            info.setStatusName(MaintenanceEnum.StatusEnum.getNameByCode(info.getStatus()));
            info.setTaskTypeName(MaintenanceEnum.TaskEmum.getNameByCode(info.getTaskType()));
        });
        //关联相关业务属性
       return new PageResult<>(list.getRecords(), list.getTotal());
    }
}
